
var global = {};

/**
 * 
 * @author Simon Auner 2011-11-28
 */
function App() {
    var me = this;
    me.init();
    me.puzzlesize = me.puzzlesizeInitValue = 3;
    
    
}

App.prototype = {

/**
 * 
 * @author Simon Auner 2011-11-28
 * @modified Aram Taieb 2011-11-29
 */
    init : function() {
        
        
        
        var me = this,
        
        countdownElContainer = document.createElement("div"),
        
        statistics = new Statistics(),
        
        timer = new CountdownTimer({
            parentEl : countdownElContainer,
            callback : function() {
                
                msg.show();
            }
        
        }),
        audio = document.createElement("audio");
        
        audio.src = "horsie1.mp3";
        
        me.timer = timer;
        countdownElContainer.className = "countdownContainer";
        document.body.appendChild(countdownElContainer);
        
        me.highScores = [];
        me.highScore = 0;
        
        var puzzle = new Puzzle({
            // Win presentation
            callbackWhenSolved : function() {          
                timer.stop();
                
                puzzle.highlightFrame();
                
                me.calcHighScore();
                
                subHeaderPointsNumber.innerText = utils.numberFormat(me.highScore);
                subHeaderLevel.innerText = me.puzzlesize - 2;
                
                statistics.setZeroPercent();
                
                timer.setText("WINNING!");
                timer.shake();
                
                setTimeout(function() {
                    winMsg.show(function() {

                        statistics.setPercent(0, me.puzzlesize);
                    });
                }, 1000);
                
            },
            scope: this,
            onHorseFaceShown: function() {
                audio.play();
            },
            levelImages : [
               "1.png", // 1
               "2.png",
               "3.png",
               "4.png",
               "5.png"
            ],
            size: 3
        });        
        
        
        
        
        
        setTimeout(function() {
            timer.start(45000);
        }, 500);
        
        
        
        var msg = new MessageBox({
            msg : "Looks like you were a little bit too slow there, buddy!",
            buttons : [
                {
                    label : "Try again",
                    callback : function() {
                        
                        puzzle.shuffle();
                        
                        timer.start(timer.getTimerStartValue());
                        
                        msg.hide();
                    }
                }
//                ,
//                {
//                    label : "Help me!",
//                    callback : function() {
//                        
//                        msg.hide();
//                        
//                        puzzle.shuffle();
//                        
//                        timer.start(timer.getTimerStartValue() + 10000);
//                        
//                        me.highScore += -10000;
//                        
//                        //console.log("highScore: " + me.highScore);
//                    }
//                }
            ]
        });
        
        var winEl = document.createElement("div"),
        subHeader = document.createElement("h2"),
        header = document.createElement("h1"),
        subHeaderText = document.createElement("span"),
        subHeaderLevel = document.createElement("span"),
        subHeaderPoints = document.createElement("h2"),
        subHeaderPointsText = document.createElement("span"),
        subHeaderPointsNumber = document.createElement("span"),
        text = document.createElement("p"),
        t = "OMG! YOU are the inspiration! You completed the puzzle! <br />";
        
        winEl.className = "winElement";
        winEl.style.cssText = "width: 700px;";
        
        header.className = "texHeader";
        header.innerText = "Congratulations!";
        
        subHeader.className = "texHeader subHeader";
        subHeaderText.innerText = "You completed level ";
        subHeader.appendChild(subHeaderText);
        subHeader.appendChild(subHeaderLevel);
        
        subHeaderPoints.className = "texHeader subHeader";
        subHeaderPointsText.innerText = "Your total score: ";
        subHeaderPoints.appendChild(subHeaderPointsText);
        subHeaderPoints.appendChild(subHeaderPointsNumber);
        
        
        text.style.cssText = "text-align: center";
        t+= "Your CCB statistics for solving the puzzle at this level:";
        text.innerHTML = t;
        
        winEl.appendChild(header);
        winEl.appendChild(subHeader);
        winEl.appendChild(subHeaderPoints);
        winEl.appendChild(text);
        
        
        winEl.appendChild(statistics.getEl());
        
        statistics.setPercent(0, 1);
        
        
        var winMsg = new MessageBox({

            el : winEl,
//           msg: "Horse says: One Netlight!",
            buttons: [
                {
                    label : "Play at this level again!",
                    callback : function() {
                        puzzle.shuffle();
                        timer.start(timer.getTimerStartValue());
                        winMsg.hide();
                    }
                },
                {
                    label : "I want MOAR deep end!",
                    callback : function() {
                        me.puzzlesize++;
                        puzzle.clearCubeGrid();
                        puzzle.createCubeGrid(me.puzzlesize);
                        puzzle.shuffle();
                        timer.start(timer.getTimerStartValue());
                        winMsg.hide();
                    }
                }
            ]
        });
    },
    
    calcHighScore : function() {
        var me = this,
        highScore = 0,
        pzlsize = me.puzzlesize,
        score = Math.round((pzlsize * me.timer.timeLeft)/1000),
        i,
        hs = me.highScores,
        hsl,
        pzlinitval = me.puzzlesizeInitValue;
        
        if (hsl < pzlsize-(pzlinitval+1)) {
            highScores.push(score);
        }
        else {
            hs[pzlsize-pzlinitval] = score;
        }       
        hsl = hs.length;
        for (i=0; i<hsl; i++) {
            highScore += hs[i];
            
            //console.log("highScore calc: " + highScore);
        }
        
        me.highScore = highScore;
    }
    
};
